package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class MessagesPeer extends BasePeer{
	public static final String tableName = "Messages";
	public static final String ID = "id";
	public static final String CHATROOMID = "chatroomId";
	public static final String FROMUSER = "fromUser";
	public static final String FROMUSERNICENAME = "fromUserNicename";
	public static final String TOUSER = "toUser";
	public static final String CONTENT = "content";
	public static final String TIME = "time";
	public static final String TYPE = "type";
	public static final String READ = "read";
	public static final String[] columnNames=new String[]{"id","chatroomId","fromUser","fromUserNicename","toUser","content","time","type","read"};
	public static Messages getMessagesByPKs(Object id, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(MessagesPeer.tableName,MessagesPeer.columnNames," TRUE  AND `id`=? ",new Object[]{id,},0, 1,conn);
			Messages messages = new Messages();
		fill(rs,messages);
		return messages;
	}
	public static Messages getMessagesReferenceColumnsOnlyByPKs(Object id, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(MessagesPeer.tableName,new String[]{"id",}," TRUE AND `id`=?",new Object[]{id,}, 0,  1,conn);
			Messages messages = new Messages();
		fill(rs,messages);
		return messages;
	}
	public static Messages getMessagesById(long id, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(MessagesPeer.tableName,MessagesPeer.columnNames,"`id`=?",new Object[]{id},0, 1,conn);
			Messages messages = new Messages();
		fill(rs,messages);
		return messages;
	}
	public static void fill(ResultSet rs, Messages messages) throws SQLException {
		if(rs.next()){
			try{messages.setId(rs.getLong("id"));}catch(Exception ex){}
			try{messages.setChatroomId(rs.getLong("chatroomId"));}catch(Exception ex){}
			try{messages.setFromUser(rs.getLong("fromUser"));}catch(Exception ex){}
			try{messages.setFromUserNicename(rs.getString("fromUserNicename"));}catch(Exception ex){}
			try{messages.setToUser(rs.getLong("toUser"));}catch(Exception ex){}
			try{messages.setContent(rs.getString("content"));}catch(Exception ex){}
			try{messages.setTime(rs.getTimestamp("time"));}catch(Exception ex){}
			try{messages.setType(rs.getInt("type"));}catch(Exception ex){}
			try{messages.setRead(rs.getBoolean("read"));}catch(Exception ex){}
			messages.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<Messages> messagesCollection) throws SQLException {
		while(rs.next()){
			Messages messages=new Messages();
			try{messages.setId(rs.getLong("id"));}catch(Exception ex){}
			try{messages.setChatroomId(rs.getLong("chatroomId"));}catch(Exception ex){}
			try{messages.setFromUser(rs.getLong("fromUser"));}catch(Exception ex){}
			try{messages.setFromUserNicename(rs.getString("fromUserNicename"));}catch(Exception ex){}
			try{messages.setToUser(rs.getLong("toUser"));}catch(Exception ex){}
			try{messages.setContent(rs.getString("content"));}catch(Exception ex){}
			try{messages.setTime(rs.getTimestamp("time"));}catch(Exception ex){}
			try{messages.setType(rs.getInt("type"));}catch(Exception ex){}
			try{messages.setRead(rs.getBoolean("read"));}catch(Exception ex){}
			messages.resetModified();
			messagesCollection.add(messages);
		}
	}
	public static boolean update(Messages messages, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < MessagesPeer.columnNames.length; i++) {
			if (((MessagesBase)messages).modified[i]) {
				columns.add(MessagesPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `Messages` SET ");
		sql.append("WHERE TRUE AND `id` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,messages.get(column));
		}
		pstmt.setObject(columns.size()+1+0,messages.get("id"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(Messages messages, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (messages.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `Messages` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (messages.get(column) != null) {
				pstmt.setObject(i + 1 + j, messages.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<Messages> getMessagesCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<Messages> collection=new Vector<Messages>();
		fill(rs,collection);
		return collection;
	}
}
